﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Tressel_asm
{
    class Label : InstructionComponentBase
    {
        /*!
        Procedure Name: Label
        Description: Overloaded constructor for Label class.  Checks syntax of string comment.
                Calls AddError from Instruction instructionReference if string labelString violates syntax
                (see error conditions).
        Input Parameters: string labelString, Instruction instructionReference
        Output Parameters: none
        Error Conditions Tested: 
                Checks the syntax of labels. Throws errors if labels are syntactically incorrect or 
                character lengths are invalid.
        Error Messages Generated:
                (See error table for specific messages)
                [2,3,4]
        Original Author: Greg Loesch and Shawn Hall
        Procedure Creation Date: 10/8/2009
        Modification Log:
        Who: Steve
        When: 10/9/2009
        Why: The property is used in the constructor, when its meant for outside the class.  I also renamed the text parameter for clarification.
        Coding standards met: SR
        Testing standards met: RM
        !*/

        // Do we need to check if it's already defined or if it's just a legal label?
        /*#
        Variable Name: minLength
        Module Where Defined: Label
        Data Type: Integer
        Local or Global: Local
        Purpose: Defines the minimum length of a label
        #*/
        private int minLength = 2;
        /*#
        Variable Name: maxLength
        Module Where Defined: Label
        Data Type: Integer
        Local or Global: Local
        Purpose: Defines the maximum length of a label
        #*/
        private int maxLength = 64;
        /*#
        Variable Name: exp
        Module Where Defined: Label
        Data Type: Regex
        Local or Global: Local
        Purpose: Creates a regular expression string to compare our labels against
        #*/
        private Regex exp = new Regex(@"^[a-zA-Z][a-zA-Z0-9\._]{1,63}$");
        /*#
        Variable Name: isGenerated
        Module Where Defined: Label
        Data Type: bool
        Local or Global: Local
        Purpose: boolean value to keep track for duplicated labels
        #*/
        private bool isGenerated = false;
        /*Procedure Name: IsGenerated
        Description: the property making isGenerated publically accessible
        Input Parameters: none
        Output Parameters: returns isGenerated
        Error Conditions Tested:
        Error Messages Generated:
        Original Author: Steve Rudy
        Procedure Creation Date: 10/17/2009
        Modification Log:
        Who: Steve Rudy
        When: 10/26/09
        Why: the label text needs to be reset if an error is called
        Coding Standards Met: SR
        Testing Standards Met: RM
        !*/
        public bool IsGenerated
        {
            get
            {
                return isGenerated;
            }
            set
            {
                isGenerated = value;
            }
        }
        public Label(string labelStringParam, Instruction instructionReference)
        {
            this.text = labelStringParam; // setting up initial property value

            if (text.Length != 0)
            {
                if (this.text.Length < minLength) // if label is too short
                {
                    instructionReference.AddError(2);
                    text = "";
                    this.errorFlag = true;
                }
                else
                {
                    if (text.Length > maxLength) // if label is too long
                    {
                        instructionReference.AddError(3);
                        text = "";
                        errorFlag = true;
                    }
                    else
                    {
                        if (!exp.IsMatch(text)) // if label does not meet syntax requirement
                        {
                            instructionReference.AddError(4);
                            text = "";
                            errorFlag = true;
                        }
                    }
                }
            }
        }
        /*!
        Procedure Name: Label (version 2)
        Description: is an empty constructor for Label
        Input Parameters: none
        Output Parameters: none
        Error Conditions Tested:
        Error Messages Generated:
        Original Author: Steve Rudy
        Procedure Creation Date: 10/19/2009
        Modification Log:
        Coding Standards Met: SR
        Testing Standards Met: RM
        !*/
        public Label()
        {
        }
    }
}
